﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.AlterManagement;
using Bricks.Business.Models.ProjectContract;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.AlterManagement;
using Bricks.Business.Data.Repositories.ProjectContract;
using Bricks.Business.Service;
using Bricks.Site.Models.DBViewModel.Contract;
using Bricks.Component.Data;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IAlterOrderService))]
    internal class AlterOrderService : IAlterOrderService
    {
        #region 属性
        [Import]
        protected IAlterOrderRepository AlterOrderRepository { get; set; }
        [Import]
        protected IAlterRepository AlterRepository { get; set; }
        [Import]
        protected IAlterOrderBackupRepository AlterOrderBackupRepository { get; set; }
        [Import]
        protected IAlterOrderBillRepository AlterOrderBillRepository { get; set; }
        [Import]
        protected IAlterOrderBillBackupRepository AlterOrderBillBackupRepository { get; set; }
        [Import]
        protected IProjectInfoRepository ProjectInfoRepository { get; set; }
        //[Import]
        //protected IContractInfoRepository ContractInfoRepository { get; set; }
        #endregion

        #region 公共函数
        private string GetRefAlterCode(string refAlterID)
        {
            string refAlterCode = string.Empty;
            if (!string.IsNullOrEmpty(refAlterID))
            {
                string[] alterIDArr = refAlterID.Split(',');
                for (int i = 0; i < alterIDArr.Length; i++)
                {
                    Guid queryID = Guid.Parse(alterIDArr[i]);
                    Alter alter = AlterRepository.Entities.FirstOrDefault(a => a.ID == queryID);
                    if (alter != null)
                    {
                        if (i == alterIDArr.Length - 1)
                        {
                            refAlterCode += "<a href='#' onclick=PopAlter('" + alterIDArr[i] + "')>" + alter.AlterCode + "</a>";
                        }
                        else
                        {
                            refAlterCode += "<a href='#' onclick=PopAlter('" + alterIDArr[i] + "')>" + alter.AlterCode + "</a>、";
                        }
                    }
                }
            }
            return refAlterCode;
        }
        #endregion

        #region 获取单据和申请没流转的过程数据和清单数据
        public AlterOrderView GetAlterOrderByID(string alterOrderID)
        {
            Guid alterOrderQueryID = Guid.Parse(alterOrderID);
            AlterOrderView alterOrderView = null;
            AlterOrder alterOrder = AlterOrderRepository.Entities.FirstOrDefault(a => a.ID == alterOrderQueryID);
            if (alterOrder != null)
            {
                alterOrderView = new AlterOrderView();
                alterOrderView.ID = alterOrder.ID;
                alterOrderView.AlterOrderID = alterOrder.ID;
                alterOrderView.FlowCode = alterOrder.FlowCode;
                alterOrderView.ProjectID = alterOrder.ProjectID;
                ProjectInfo projectInfo = ProjectInfoRepository.Entities.SingleOrDefault(p => p.ID == alterOrder.ProjectID);
                if (projectInfo != null)
                {
                    alterOrderView.ProjectName = projectInfo.ProjectName;
                }
                alterOrderView.ContractID = alterOrder.ContractID;
                alterOrderView.AlterOrderCode = alterOrder.AlterOrderCode;
                alterOrderView.AlterOrderName = alterOrder.AlterOrderName;
                alterOrderView.PileNo = alterOrder.PileNo;
                alterOrderView.AlterPosition = alterOrder.AlterPosition;
                alterOrderView.ApprovalNumber = alterOrder.ApprovalNumber;
                alterOrderView.ApprovalDate = alterOrder.ApprovalDate;
                alterOrderView.AlterType = alterOrder.AlterType;
                alterOrderView.RefAlterID = alterOrder.RefAlterID;
                alterOrderView.RefAlterCode = GetRefAlterCode(alterOrder.RefAlterID);
                alterOrderView.OriginalChartName = alterOrder.OriginalChartName;
                alterOrderView.AlterChartName = alterOrder.AlterChartName;
                alterOrderView.OriginalChartNo = alterOrder.OriginalChartNo;
                alterOrderView.AlterChartNo = alterOrder.AlterChartNo;
                alterOrderView.AlterReason = alterOrder.AlterReason;
                alterOrderView.AlterMoney = alterOrder.AlterMoney;
                alterOrderView.AlterDate = alterOrder.AlterDate;
                alterOrderView.RequestPerson = alterOrder.RequestPerson;
                alterOrderView.RequestDate = alterOrder.RequestDate;
                alterOrderView.HandleState = alterOrder.HandleState;
                alterOrderView.AssociatedNodeCode = "";
                alterOrderView.LastUpdateUserDepartment = alterOrder.LastUpdateUserDepartment;
                alterOrderView.LastUpdateUser = alterOrder.LastUpdateUser;
                alterOrderView.LastUpdateTime = alterOrder.LastUpdateTime;
                alterOrderView.SaveState = "0";
                alterOrderView.SendState = "1";
            }
            else
            {
                AlterOrderBackup alterOrderBackup = AlterOrderBackupRepository.Entities.FirstOrDefault(a => a.AlterOrderID == alterOrderQueryID);
                if (alterOrderBackup != null)
                {
                    alterOrderView = new AlterOrderView();
                    alterOrderView.ID = alterOrderBackup.ID;
                    alterOrderView.AlterOrderID = alterOrderBackup.AlterOrderID;
                    alterOrderView.FlowCode = alterOrderBackup.FlowCode;
                    alterOrderView.ProjectID = alterOrderBackup.ProjectID;
                    ProjectInfo projectInfo = ProjectInfoRepository.Entities.SingleOrDefault(p => p.ID == alterOrderBackup.ProjectID);
                    if (projectInfo != null)
                    {
                        alterOrderView.ProjectName = projectInfo.ProjectName;
                    }
                    alterOrderView.ContractID = alterOrderBackup.ContractID;
                    alterOrderView.AlterOrderCode = alterOrderBackup.AlterOrderCode;
                    alterOrderView.AlterOrderName = alterOrderBackup.AlterOrderName;
                    alterOrderView.PileNo = alterOrderBackup.PileNo;
                    alterOrderView.AlterPosition = alterOrderBackup.AlterPosition;
                    alterOrderView.ApprovalNumber = alterOrderBackup.ApprovalNumber;
                    alterOrderView.ApprovalDate = alterOrderBackup.ApprovalDate;
                    alterOrderView.AlterType = alterOrderBackup.AlterType;
                    alterOrderView.RefAlterID = alterOrderBackup.RefAlterID;
                    alterOrderView.RefAlterCode = GetRefAlterCode(alterOrderBackup.RefAlterID);
                    alterOrderView.OriginalChartName = alterOrderBackup.OriginalChartName;
                    alterOrderView.AlterChartName = alterOrderBackup.AlterChartName;
                    alterOrderView.OriginalChartNo = alterOrderBackup.OriginalChartNo;
                    alterOrderView.AlterChartNo = alterOrderBackup.AlterChartNo;
                    alterOrderView.AlterReason = alterOrderBackup.AlterReason;
                    alterOrderView.AlterMoney = alterOrderBackup.AlterMoney;
                    alterOrderView.AlterDate = alterOrderBackup.AlterDate;
                    alterOrderView.RequestPerson = alterOrderBackup.RequestPerson;
                    alterOrderView.RequestDate = alterOrderBackup.RequestDate;
                    alterOrderView.HandleState = alterOrderBackup.HandleState;
                    alterOrderView.AssociatedNodeCode = alterOrderBackup.AssociatedNodeCode;
                    alterOrderView.LastUpdateUserDepartment = alterOrderBackup.LastUpdateUserDepartment;
                    alterOrderView.LastUpdateUser = alterOrderBackup.LastUpdateUser;
                    alterOrderView.LastUpdateTime = alterOrderBackup.LastUpdateTime;
                    alterOrderView.SaveState = alterOrderBackup.SaveState;
                    alterOrderView.SendState = alterOrderBackup.SendState;
                }
            }
            return alterOrderView;
        }

        public List<AlterOrderBillView> GetAlterOrderBillList(Guid alterOrderID)
        {
            IQueryable<AlterOrderBill> alterOrderBills = AlterOrderBillRepository.Entities.Where(a => a.PID == alterOrderID);
            List<AlterOrderBillView> alterOrderBillViewList = new List<AlterOrderBillView>();
            foreach (AlterOrderBill alterOrderBill in alterOrderBills)
            {
                alterOrderBillViewList.Add(new AlterOrderBillView()
                {
                    ID = GuidHelper.NewGuid(),
                    PID = alterOrderBill.PID,
                    AlterOrderBillID = alterOrderBill.ID,
                    BillCode = alterOrderBill.BillCode,
                    BillName = alterOrderBill.BillName,
                    Unit = alterOrderBill.Unit,
                    OriginalCount = alterOrderBill.OriginalCount,
                    AfterAlterCount = alterOrderBill.AfterAlterCount,
                    AlterCount = alterOrderBill.AlterCount,
                    Price=alterOrderBill.Price,
                    AlterMoney = alterOrderBill.AlterMoney,
                    Remark = alterOrderBill.Remark,
                    AssociatedNodeCode = "",
                    LastUpdateUserDepartment = alterOrderBill.LastUpdateUserDepartment,
                    LastUpdateUser = alterOrderBill.LastUpdateUser,
                    LastUpdateTime = alterOrderBill.LastUpdateTime,
                    SendState = "0"
                });
            }
            return alterOrderBillViewList;
        }
        #endregion

        #region 获取单据和清单的过程数据
        public AlterOrderView GetAlterOrderBackupByID(Guid id)
        {
            AlterOrderView alterOrderView = null;
            AlterOrderBackup alterOrderBackup = AlterOrderBackupRepository.Entities.FirstOrDefault(a => a.ID == id);
            if (alterOrderBackup != null)
            {
                alterOrderView = new AlterOrderView();
                alterOrderView.ID = alterOrderBackup.ID;
                alterOrderView.AlterOrderID = alterOrderBackup.AlterOrderID;
                alterOrderView.FlowCode = alterOrderBackup.FlowCode;
                alterOrderView.ProjectID = alterOrderBackup.ProjectID;
                ProjectInfo projectInfo = ProjectInfoRepository.Entities.SingleOrDefault(p => p.ID == alterOrderBackup.ProjectID);
                if (projectInfo != null)
                {
                    alterOrderView.ProjectName = projectInfo.ProjectName;
                }
                alterOrderView.ContractID = alterOrderBackup.ContractID;
                alterOrderView.AlterOrderCode = alterOrderBackup.AlterOrderCode;
                alterOrderView.AlterOrderName = alterOrderBackup.AlterOrderName;
                alterOrderView.PileNo = alterOrderBackup.PileNo;
                alterOrderView.AlterPosition = alterOrderBackup.AlterPosition;
                alterOrderView.ApprovalNumber = alterOrderBackup.ApprovalNumber;
                alterOrderView.ApprovalDate = alterOrderBackup.ApprovalDate;
                alterOrderView.AlterType = alterOrderBackup.AlterType;
                alterOrderView.RefAlterID = alterOrderBackup.RefAlterID;
                alterOrderView.RefAlterCode = GetRefAlterCode(alterOrderBackup.RefAlterID);
                alterOrderView.OriginalChartName = alterOrderBackup.OriginalChartName;
                alterOrderView.AlterChartName = alterOrderBackup.AlterChartName;
                alterOrderView.OriginalChartNo = alterOrderBackup.OriginalChartNo;
                alterOrderView.AlterChartNo = alterOrderBackup.AlterChartNo;
                alterOrderView.AlterReason = alterOrderBackup.AlterReason;
                alterOrderView.AlterMoney = alterOrderBackup.AlterMoney;
                alterOrderView.AlterDate = alterOrderBackup.AlterDate;
                alterOrderView.RequestPerson = alterOrderBackup.RequestPerson;
                alterOrderView.RequestDate = alterOrderBackup.RequestDate;
                alterOrderView.HandleState = alterOrderBackup.HandleState;
                alterOrderView.AssociatedNodeCode = alterOrderBackup.AssociatedNodeCode;
                alterOrderView.LastUpdateUserDepartment = alterOrderBackup.LastUpdateUserDepartment;
                alterOrderView.LastUpdateUser = alterOrderBackup.LastUpdateUser;
                alterOrderView.LastUpdateTime = alterOrderBackup.LastUpdateTime;
                alterOrderView.SaveState = alterOrderBackup.SaveState;
                alterOrderView.SendState = alterOrderBackup.SendState;
            }
            return alterOrderView;
        }

        public AlterOrderView GetAlterOrderBackupByAlterOrderID(string alterOrderID)
        {
            Guid queryID = Guid.Parse(alterOrderID);
            AlterOrderView alterOrderView = null;
            AlterOrderBackup alterOrderBackup  = AlterOrderBackupRepository.Entities.Where(a => a.AlterOrderID == queryID && a.SendState == "1").OrderByDescending(a => a.LastUpdateTime).FirstOrDefault();
            if (alterOrderBackup != null)
            {
                alterOrderView = new AlterOrderView();
                alterOrderView.ID = alterOrderBackup.ID;
                alterOrderView.AlterOrderID = alterOrderBackup.AlterOrderID;
                alterOrderView.FlowCode = alterOrderBackup.FlowCode;
                alterOrderView.ProjectID = alterOrderBackup.ProjectID;
                ProjectInfo projectInfo = ProjectInfoRepository.Entities.SingleOrDefault(p => p.ID == alterOrderBackup.ProjectID);
                if (projectInfo != null)
                {
                    alterOrderView.ProjectName = projectInfo.ProjectName;
                }
                alterOrderView.ContractID = alterOrderBackup.ContractID;
                alterOrderView.AlterOrderCode = alterOrderBackup.AlterOrderCode;
                alterOrderView.AlterOrderName = alterOrderBackup.AlterOrderName;
                alterOrderView.PileNo = alterOrderBackup.PileNo;
                alterOrderView.AlterPosition = alterOrderBackup.AlterPosition;
                alterOrderView.ApprovalNumber = alterOrderBackup.ApprovalNumber;
                alterOrderView.ApprovalDate = alterOrderBackup.ApprovalDate;
                alterOrderView.AlterType = alterOrderBackup.AlterType;
                alterOrderView.RefAlterID = alterOrderBackup.RefAlterID;
                alterOrderView.RefAlterCode = GetRefAlterCode(alterOrderBackup.RefAlterID);
                alterOrderView.OriginalChartName = alterOrderBackup.OriginalChartName;
                alterOrderView.AlterChartName = alterOrderBackup.AlterChartName;
                alterOrderView.OriginalChartNo = alterOrderBackup.OriginalChartNo;
                alterOrderView.AlterChartNo = alterOrderBackup.AlterChartNo;
                alterOrderView.AlterReason = alterOrderBackup.AlterReason;
                alterOrderView.AlterMoney = alterOrderBackup.AlterMoney;
                alterOrderView.AlterDate = alterOrderBackup.AlterDate;
                alterOrderView.RequestPerson = alterOrderBackup.RequestPerson;
                alterOrderView.RequestDate = alterOrderBackup.RequestDate;
                alterOrderView.HandleState = alterOrderBackup.HandleState;
                alterOrderView.AssociatedNodeCode = alterOrderBackup.AssociatedNodeCode;
                alterOrderView.LastUpdateUserDepartment = alterOrderBackup.LastUpdateUserDepartment;
                alterOrderView.LastUpdateUser = alterOrderBackup.LastUpdateUser;
                alterOrderView.LastUpdateTime = alterOrderBackup.LastUpdateTime;
                alterOrderView.SaveState = alterOrderBackup.SaveState;
                alterOrderView.SendState = alterOrderBackup.SendState;
            }
            return alterOrderView;
        }

        public AlterOrderView GetAlterOrderBackupByCondition(string alterOrderID, string nodeCode, string department, string userName)
        {
            Guid queryID = Guid.Parse(alterOrderID);
            AlterOrderView alterOrderView = null;
            AlterOrderBackup alterOrderBackup = AlterOrderBackupRepository.Entities.FirstOrDefault(a => a.AlterOrderID == queryID && a.AssociatedNodeCode == nodeCode && a.LastUpdateUserDepartment == department && a.LastUpdateUser == userName);
            if (alterOrderBackup != null)
            {
                alterOrderView = new AlterOrderView();
                alterOrderView.ID = alterOrderBackup.ID;
                alterOrderView.AlterOrderID = alterOrderBackup.AlterOrderID;
                alterOrderView.FlowCode = alterOrderBackup.FlowCode;
                alterOrderView.ProjectID = alterOrderBackup.ProjectID;
                ProjectInfo projectInfo = ProjectInfoRepository.Entities.SingleOrDefault(p => p.ID == alterOrderBackup.ProjectID);
                if (projectInfo != null)
                {
                    alterOrderView.ProjectName = projectInfo.ProjectName;
                }
                alterOrderView.ContractID = alterOrderBackup.ContractID;
                alterOrderView.AlterOrderCode = alterOrderBackup.AlterOrderCode;
                alterOrderView.AlterOrderName = alterOrderBackup.AlterOrderName;
                alterOrderView.PileNo = alterOrderBackup.PileNo;
                alterOrderView.AlterPosition = alterOrderBackup.AlterPosition;
                alterOrderView.ApprovalNumber = alterOrderBackup.ApprovalNumber;
                alterOrderView.ApprovalDate = alterOrderBackup.ApprovalDate;
                alterOrderView.AlterType = alterOrderBackup.AlterType;
                alterOrderView.RefAlterID = alterOrderBackup.RefAlterID;
                alterOrderView.RefAlterCode = GetRefAlterCode(alterOrderBackup.RefAlterID);
                alterOrderView.OriginalChartName = alterOrderBackup.OriginalChartName;
                alterOrderView.AlterChartName = alterOrderBackup.AlterChartName;
                alterOrderView.OriginalChartNo = alterOrderBackup.OriginalChartNo;
                alterOrderView.AlterChartNo = alterOrderBackup.AlterChartNo;
                alterOrderView.AlterReason = alterOrderBackup.AlterReason;
                alterOrderView.AlterMoney = alterOrderBackup.AlterMoney;
                alterOrderView.AlterDate = alterOrderBackup.AlterDate;
                alterOrderView.RequestPerson = alterOrderBackup.RequestPerson;
                alterOrderView.RequestDate = alterOrderBackup.RequestDate;
                alterOrderView.HandleState = alterOrderBackup.HandleState;
                alterOrderView.AssociatedNodeCode = alterOrderBackup.AssociatedNodeCode;
                alterOrderView.LastUpdateUserDepartment = alterOrderBackup.LastUpdateUserDepartment;
                alterOrderView.LastUpdateUser = alterOrderBackup.LastUpdateUser;
                alterOrderView.LastUpdateTime = alterOrderBackup.LastUpdateTime;
                alterOrderView.SaveState = alterOrderBackup.SaveState;
                alterOrderView.SendState = alterOrderBackup.SendState;
            }
            return alterOrderView;
        }

        public List<AlterOrderBillView> GetAlterOrderBillBackupList(Guid alterOrderID, string nodeCode, string department, string userName)
        {
            IQueryable<AlterOrderBillBackup> alterOrderBillBackups = AlterOrderBillBackupRepository.Entities.Where(a => a.PID == alterOrderID && a.AssociatedNodeCode == nodeCode && a.LastUpdateUserDepartment == department && a.LastUpdateUser == userName);
            List<AlterOrderBillView> alterOrderBillViewList = new List<AlterOrderBillView>();
            foreach (AlterOrderBillBackup alterOrderBillBackup in alterOrderBillBackups)
            {
                alterOrderBillViewList.Add(new AlterOrderBillView()
                {
                    ID = alterOrderBillBackup.ID,
                    PID = alterOrderBillBackup.PID,
                    AlterOrderBillID = alterOrderBillBackup.AlterOrderBillID,
                    BillCode = alterOrderBillBackup.BillCode,
                    BillName = alterOrderBillBackup.BillName,
                    Unit = alterOrderBillBackup.Unit,
                    OriginalCount = alterOrderBillBackup.OriginalCount,
                    AfterAlterCount = alterOrderBillBackup.AfterAlterCount,
                    AlterCount = alterOrderBillBackup.AlterCount,
                    Price=alterOrderBillBackup.Price,
                    AlterMoney = alterOrderBillBackup.AlterMoney,
                    Remark = alterOrderBillBackup.Remark,
                    AssociatedNodeCode = alterOrderBillBackup.AssociatedNodeCode,
                    LastUpdateUserDepartment = alterOrderBillBackup.LastUpdateUserDepartment,
                    LastUpdateUser = alterOrderBillBackup.LastUpdateUser,
                    LastUpdateTime = alterOrderBillBackup.LastUpdateTime,
                    SendState = alterOrderBillBackup.SendState
                });
            }
            return alterOrderBillViewList;
        }
        #endregion

        #region 保存单据和清单数据
        public void SaveAlterOrder(AlterOrderView alterOrderView, List<AlterOrderBillView> saveList)
        {
            AlterOrder record = AlterOrderRepository.Entities.FirstOrDefault(a => a.ID == alterOrderView.AlterOrderID);
            if (record != null)
            {
                UpdateAlterOrder(record, alterOrderView);
            }
            else
            {
                InsertAlterOrder(alterOrderView);
            }

            DeleteAlterOrderBill(alterOrderView);
            foreach (AlterOrderBillView alterOrderBillView in saveList)
            {
                InsertAlterOrderBill(alterOrderBillView);
            }
        }

        private void InsertAlterOrder(AlterOrderView alterOrderView)
        {
            AlterOrder record = new AlterOrder()
            {
                ID = alterOrderView.AlterOrderID,
                FlowCode = alterOrderView.FlowCode,
                ProjectID = alterOrderView.ProjectID,
                ContractID = alterOrderView.ContractID,
                AlterOrderCode = alterOrderView.AlterOrderCode,
                AlterOrderName = alterOrderView.AlterOrderName,
                PileNo=alterOrderView.PileNo,
                AlterPosition = alterOrderView.AlterPosition,
                ApprovalNumber=alterOrderView.ApprovalNumber,
                ApprovalDate=alterOrderView.ApprovalDate,
                AlterType = alterOrderView.AlterType,
                RefAlterID = alterOrderView.RefAlterID,
                OriginalChartName = alterOrderView.OriginalChartName,
                AlterChartName = alterOrderView.AlterChartName,
                OriginalChartNo = alterOrderView.OriginalChartNo,
                AlterChartNo = alterOrderView.AlterChartNo,
                AlterReason = alterOrderView.AlterReason,
                AlterMoney = alterOrderView.AlterMoney,
                AlterDate = alterOrderView.AlterDate,
                RequestPerson = alterOrderView.RequestPerson,
                RequestDate = alterOrderView.RequestDate,
                HandleState = alterOrderView.HandleState,
                LastUpdateUserDepartment = alterOrderView.LastUpdateUserDepartment,
                LastUpdateUser = alterOrderView.LastUpdateUser,
                LastUpdateTime = alterOrderView.LastUpdateTime
            };
            AlterOrderRepository.Insert(record);
        }

        private void UpdateAlterOrder(AlterOrder record, AlterOrderView alterOrderView)
        {
            record.ProjectID = alterOrderView.ProjectID;
            record.ContractID = alterOrderView.ContractID;
            record.AlterOrderCode = alterOrderView.AlterOrderCode;
            record.AlterOrderName = alterOrderView.AlterOrderName;
            record.PileNo = alterOrderView.PileNo;
            record.AlterPosition = alterOrderView.AlterPosition;
            record.ApprovalNumber = alterOrderView.ApprovalNumber;
            record.ApprovalDate = alterOrderView.ApprovalDate;
            record.AlterType = alterOrderView.AlterType;
            record.RefAlterID = alterOrderView.RefAlterID;
            record.OriginalChartName = alterOrderView.OriginalChartName;
            record.AlterChartName = alterOrderView.AlterChartName;
            record.OriginalChartNo = alterOrderView.OriginalChartNo;
            record.AlterChartNo = alterOrderView.AlterChartNo;
            record.AlterReason = alterOrderView.AlterReason;
            record.AlterMoney = alterOrderView.AlterMoney;
            record.AlterDate = alterOrderView.AlterDate;
            record.HandleState = alterOrderView.HandleState;
            record.LastUpdateUserDepartment = alterOrderView.LastUpdateUserDepartment;
            record.LastUpdateUser = alterOrderView.LastUpdateUser;
            record.LastUpdateTime = alterOrderView.LastUpdateTime;
            AlterOrderRepository.Update(record);
        }

        private void DeleteAlterOrderBill(AlterOrderView alterOrderView)
        {
            IQueryable<AlterOrderBill> lstAlterOrderBill = AlterOrderBillRepository.Entities.Where(a => a.PID == alterOrderView.AlterOrderID);
            List<Guid> lstDeleteID = new List<Guid>();
            foreach (AlterOrderBill alterOrderBill in lstAlterOrderBill)
            {
                lstDeleteID.Add(alterOrderBill.ID);
            }
            foreach (Guid id in lstDeleteID)
            {
                AlterOrderBillRepository.Delete(id);
            }
        }

        private void InsertAlterOrderBill(AlterOrderBillView alterOrderBillView)
        {
            AlterOrderBill record = new AlterOrderBill()
            {
                ID = alterOrderBillView.AlterOrderBillID,
                PID = alterOrderBillView.PID,
                BillCode = alterOrderBillView.BillCode,
                BillName = alterOrderBillView.BillName,
                Unit = alterOrderBillView.Unit,
                OriginalCount = alterOrderBillView.OriginalCount,
                AfterAlterCount = alterOrderBillView.AfterAlterCount,
                AlterCount = alterOrderBillView.AlterCount,
                Price=alterOrderBillView.Price,
                AlterMoney = alterOrderBillView.AlterMoney,
                Remark = alterOrderBillView.Remark,
                LastUpdateUserDepartment = alterOrderBillView.LastUpdateUserDepartment,
                LastUpdateUser = alterOrderBillView.LastUpdateUser,
                LastUpdateTime = alterOrderBillView.LastUpdateTime
            };
            AlterOrderBillRepository.Insert(record);
        }
        #endregion

        #region 保存单据和清单的过程数据
        public void SaveAlterOrderBackup(AlterOrderView alterOrderView, List<AlterOrderBillView> saveList)
        {
            AlterOrderBackup record = AlterOrderBackupRepository.Entities.FirstOrDefault(a => a.AlterOrderID == alterOrderView.AlterOrderID && a.AssociatedNodeCode == alterOrderView.AssociatedNodeCode && a.LastUpdateUserDepartment == alterOrderView.LastUpdateUserDepartment && a.LastUpdateUser == alterOrderView.LastUpdateUser);
            if (record != null)
            {
                UpdateAlterOrderBackup(record, alterOrderView);
            }
            else
            {
                InsertAlterOrderBackup(alterOrderView);
            }

            DeleteAlterOrderBillBackup(alterOrderView);
            foreach (AlterOrderBillView alterOrderBillView in saveList)
            {
                InsertAlterOrderBillBackup(alterOrderBillView);
            }
        }

        private void InsertAlterOrderBackup(AlterOrderView alterOrderView)
        {
            AlterOrderBackup record = new AlterOrderBackup()
            {
                ID = alterOrderView.ID,
                FlowCode = alterOrderView.FlowCode,
                AlterOrderID = alterOrderView.AlterOrderID,
                ProjectID = alterOrderView.ProjectID,
                ContractID = alterOrderView.ContractID,
                AlterOrderCode = alterOrderView.AlterOrderCode,
                AlterOrderName = alterOrderView.AlterOrderName,
                PileNo = alterOrderView.PileNo,
                AlterPosition = alterOrderView.AlterPosition,
                ApprovalNumber = alterOrderView.ApprovalNumber,
                ApprovalDate=alterOrderView.ApprovalDate,
                AlterType = alterOrderView.AlterType,
                RefAlterID = alterOrderView.RefAlterID,
                OriginalChartName = alterOrderView.OriginalChartName,
                AlterChartName = alterOrderView.AlterChartName,
                OriginalChartNo = alterOrderView.OriginalChartNo,
                AlterChartNo = alterOrderView.AlterChartNo,
                AlterReason = alterOrderView.AlterReason,
                AlterMoney = alterOrderView.AlterMoney,
                AlterDate = alterOrderView.AlterDate,
                RequestPerson = alterOrderView.RequestPerson,
                RequestDate = alterOrderView.RequestDate,
                HandleState = alterOrderView.HandleState,
                AssociatedNodeCode = alterOrderView.AssociatedNodeCode,
                LastUpdateUserDepartment = alterOrderView.LastUpdateUserDepartment,
                LastUpdateUser = alterOrderView.LastUpdateUser,
                LastUpdateTime = alterOrderView.LastUpdateTime,
                SaveState = alterOrderView.SaveState,
                SendState = alterOrderView.SendState
            };
            AlterOrderBackupRepository.Insert(record);
        }

        private void UpdateAlterOrderBackup(AlterOrderBackup record, AlterOrderView alterOrderView)
        {
            record.ProjectID = alterOrderView.ProjectID;
            record.ContractID = alterOrderView.ContractID;
            record.AlterOrderCode = alterOrderView.AlterOrderCode;
            record.AlterOrderName = alterOrderView.AlterOrderName;
            record.PileNo = alterOrderView.PileNo;
            record.AlterPosition = alterOrderView.AlterPosition;
            record.ApprovalNumber = alterOrderView.ApprovalNumber;
            record.ApprovalDate = alterOrderView.ApprovalDate;
            record.AlterType = alterOrderView.AlterType;
            record.RefAlterID = alterOrderView.RefAlterID;
            record.OriginalChartName = alterOrderView.OriginalChartName;
            record.AlterChartName = alterOrderView.AlterChartName;
            record.OriginalChartNo = alterOrderView.OriginalChartNo;
            record.AlterChartNo = alterOrderView.AlterChartNo;
            record.AlterReason = alterOrderView.AlterReason;
            record.AlterMoney = alterOrderView.AlterMoney;
            record.AlterDate = alterOrderView.AlterDate;
            record.HandleState = alterOrderView.HandleState;
            record.LastUpdateTime = alterOrderView.LastUpdateTime;
            record.SaveState = alterOrderView.SaveState;
            record.SendState = alterOrderView.SendState;
            AlterOrderBackupRepository.Update(record);
        }

        private void DeleteAlterOrderBillBackup(AlterOrderView alterOrderView)
        {
            IQueryable<AlterOrderBillBackup> lstAlterOrderBillBackup = AlterOrderBillBackupRepository.Entities.Where(a => a.PID == alterOrderView.AlterOrderID && a.AssociatedNodeCode == alterOrderView.AssociatedNodeCode && a.LastUpdateUserDepartment == alterOrderView.LastUpdateUserDepartment && a.LastUpdateUser == alterOrderView.LastUpdateUser);
            List<Guid> lstDeleteID = new List<Guid>();
            foreach (AlterOrderBillBackup alterBillBackup in lstAlterOrderBillBackup)
            {
                lstDeleteID.Add(alterBillBackup.ID);
            }
            foreach (Guid id in lstDeleteID)
            {
                AlterOrderBillBackupRepository.Delete(id);
            }
        }

        private void InsertAlterOrderBillBackup(AlterOrderBillView alterOrderBillView)
        {
            AlterOrderBillBackup record = new AlterOrderBillBackup()
            {
                ID = GuidHelper.NewGuid(),
                PID = alterOrderBillView.PID,
                AlterOrderBillID = alterOrderBillView.AlterOrderBillID,
                BillCode = alterOrderBillView.BillCode,
                BillName = alterOrderBillView.BillName,
                Unit = alterOrderBillView.Unit,
                OriginalCount = alterOrderBillView.OriginalCount,
                AfterAlterCount = alterOrderBillView.AfterAlterCount,
                AlterCount = alterOrderBillView.AlterCount,
                Price=alterOrderBillView.Price,
                AlterMoney = alterOrderBillView.AlterMoney,
                Remark = alterOrderBillView.Remark,
                AssociatedNodeCode = alterOrderBillView.AssociatedNodeCode,
                LastUpdateUserDepartment = alterOrderBillView.LastUpdateUserDepartment,
                LastUpdateUser = alterOrderBillView.LastUpdateUser,
                LastUpdateTime = alterOrderBillView.LastUpdateTime,
                SendState = alterOrderBillView.SendState
            };
            AlterOrderBillBackupRepository.Insert(record);
        }
        #endregion

        #region 单据完成或者终止之后，删除所有sendState="0"的数据
        public void DeleteAlterOrderProcessData(string alterOrderID)
        {
            string sql = @"DELETE FROM AlterOrderBackups WHERE AlterOrderID='" + alterOrderID + "' AND ISNULL(SendState,'0')='0';DELETE FROM AlterOrderBillBackups WHERE PID='" + alterOrderID + "' AND ISNULL(SendState,'0')='0';";
            AlterOrderRepository.ExeSqlCommon(sql);
        }

        #endregion

        #region 撤回或者回退的时候处理单据的业务数据
        public void WithdrawOrBackAlterOrderForRequestNode(string alterOrderID, string nodeCode)
        {
            string sql = @"DELETE FROM AlterOrders WHERE ID='" + alterOrderID + "';" +
                          "DELETE FROM AlterOrderBills WHERE PID='" + alterOrderID + "';" +
                          "DELETE FROM AlterOrderBackups WHERE AlterOrderID='" + alterOrderID + "' AND AssociatedNodeCode<>'" + nodeCode + "';" +
                          "DELETE FROM AlterOrderBillBackups WHERE PID='" + alterOrderID + "' AND AssociatedNodeCode<>'" + nodeCode + "';" +
                          "UPDATE AlterOrderBackups SET HandleState='0',SaveState='1',SendState='0' WHERE AlterOrderID='" + alterOrderID + "';" +
                          "UPDATE AlterOrderBillBackups SET SendState='0' WHERE PID='" + alterOrderID + "';";
            AlterOrderRepository.ExeSqlCommon(sql);
        }

        public void WithdrawOrBackAlterOrderForOtherNode(string alterOrderID, string nodeCode, string nodeCodeStr)
        {
            string sql = @"DELETE FROM AlterOrderBills WHERE PID='" + alterOrderID + "';" +
                          "DELETE FROM AlterOrderBackups WHERE AlterOrderID='" + alterOrderID + "' AND AssociatedNodeCode IN (" + nodeCodeStr + ");" +
                          "DELETE FROM AlterOrderBillBackups WHERE PID='" + alterOrderID + "' AND AssociatedNodeCode IN (" + nodeCodeStr + ");" +
                          "UPDATE AlterOrderBackups SET SendState='0' WHERE AlterOrderID='" + alterOrderID + "' AND AssociatedNodeCode='" + nodeCode + "';" +
                          "UPDATE AlterOrderBillBackups SET SendState='0' WHERE PID='" + alterOrderID + "'AND AssociatedNodeCode='" + nodeCode + "';";
            AlterOrderRepository.ExeSqlCommon(sql);
        }
        #endregion

        #region 过程管理列表的界面数据展示和删除选中项
        public List<AlterOrderView> GetAlterOrderList(Guid projectID, Guid contractID)
        {
            //string sql = "select * from V_ContractInfoes";
            //var lstContractInfo = DbSqlHelper<VMContractInfo>.SqlQueryCommon(sql);

            var queryList = from a in AlterOrderRepository.Entities
                            where a.ProjectID==projectID && a.ContractID==contractID
                            select a;

            List<AlterOrderView> alterOrderViewList = new List<AlterOrderView>();
            foreach (var query in queryList)
            {
                alterOrderViewList.Add(new AlterOrderView()
                {
                    ID = GuidHelper.NewGuid(),
                    AlterOrderID = query.ID,
                    FlowCode = query.FlowCode,
                    ContractID=query.ContractID,
                    AlterOrderCode = query.AlterOrderCode,
                    AlterOrderName = query.AlterOrderName,
                    AlterType = query.AlterType,
                    AlterMoney = query.AlterMoney,
                    AlterDate = query.AlterDate,
                    RequestDate = query.RequestDate,
                    HandleState = query.HandleState
                });
            }

            var queryProcessList = from a in AlterOrderBackupRepository.Entities
                                   where a.HandleState == "0" &&a.ProjectID==projectID && a.ContractID==contractID
                                   select a;

            foreach (var query in queryProcessList)
            {
                alterOrderViewList.Add(new AlterOrderView()
                {
                    ID = GuidHelper.NewGuid(),
                    AlterOrderID = query.AlterOrderID,
                    FlowCode = query.FlowCode,
                    ContractID = query.ContractID,
                    AlterOrderCode = query.AlterOrderCode,
                    AlterOrderName = query.AlterOrderName,
                    AlterType = query.AlterType,
                    AlterMoney = query.AlterMoney,
                    AlterDate = query.AlterDate,
                    RequestDate = query.RequestDate,
                    HandleState = query.HandleState
                });
            }

            return alterOrderViewList;
        }

        public OperationResult DeleteSelectAlterOrder(string alterOrderID)
        {
            try
            {
                PublicHelper.CheckArgument(alterOrderID, "id");
                Guid guidID = Guid.Parse(alterOrderID);
                AlterOrder record = AlterOrderRepository.Entities.FirstOrDefault(m => m.ID == guidID);
                AlterOrderBackup recordBackup = AlterOrderBackupRepository.Entities.FirstOrDefault(m => m.AlterOrderID == guidID);
                if (record == null && recordBackup == null)
                    return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");
                if (record != null)
                {
                    AlterOrderRepository.Delete(record);
                }

                string sql = @"DELETE FROM AlterOrderBills WHERE PID='" + alterOrderID + "';" +
                              "DELETE FROM AlterOrderBackups WHERE AlterOrderID='" + alterOrderID + "';" +
                              "DELETE FROM AlterOrderBillBackups WHERE PID='" + alterOrderID + "';" +
                              "DELETE FROM BaklogTasks WHERE RecordID='" + alterOrderID + "';" +
                              "DELETE FROM BaklogTaskTemps WHERE RecordID='" + alterOrderID + "';" +
                              "DELETE FROM CompleteFlowNodes WHERE RecordID='" + alterOrderID + "';" +
                              "DELETE FROM FlowGraphNodes WHERE RecordID='" + alterOrderID + "';" +
                              "DELETE FROM FlowProcessMessages WHERE RecordID='" + alterOrderID + "';" +
                              "DELETE FROM ProcessedTasks WHERE RecordID='" + alterOrderID + "';";
                AlterOrderRepository.ExeSqlCommon(sql);
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            }
            catch (Exception ex)
            {
                return new OperationResult(OperationResultType.Error, "删除失败:" + ex.Message);
            }
        }
        #endregion
    }
}